package com.zim.ta_service.base.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zim.ta_service.base.holder.UserContextHolder;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * author: alabimofa <br>
 * date: 2021/10/22 21:01 <br>
 */
@Component
public class MybatisMetaObjectHandler implements MetaObjectHandler {

	@Override
	public void insertFill(MetaObject metaObject) {
		this.strictInsertFill(metaObject, "createdDate", Date::new, Date.class);
		this.strictInsertFill(metaObject, "createdBy", UserContextHolder::getUserCode, String.class);
		this.strictInsertFill(metaObject, "lastUpdatedDate", Date::new, Date.class);
		this.strictInsertFill(metaObject, "lastUpdatedBy", UserContextHolder::getUserCode, String.class);
	}

	@Override
	public void updateFill(MetaObject metaObject) {
		this.setFieldValByName("lastUpdatedDate", new Date(), metaObject);
//		this.strictUpdateFill(metaObject, "lastUpdatedDate", Date::new, Date.class);
		this.strictUpdateFill(metaObject, "lastUpdatedBy", UserContextHolder::getUserCode, String.class);
	}
}
